﻿using Dapper;
using Domain.Entity;
using Domain.Repository;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class CM2Repository : ICM2Repository
    {
        private readonly MyDbContext myDbContext;

        public CM2Repository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<int> AddCM2(CM cM)
        {
            myDbContext.CM.Add(cM);
            return await myDbContext.SaveChangesAsync();
        }

		public List<CM> CMName(string name)
		{
			var zhi = myDbContext.CM.Where(e => e.MajorKindName == name).ToList();
			return zhi;
		}

		public async Task<int> DeleteCM2(int id)
        {
            var CM = await myDbContext.CM.FindAsync(id);
            if (CM != null)
            {
                myDbContext.CM.Remove(CM);
                return await myDbContext.SaveChangesAsync();
            }
            return 0;
        }

        public List<CM> GetCM2()
        {
            using (SqlConnection connection = new SqlConnection("server=.;database=HR;user=sa;pwd=cssl#123;Encrypt=False;"))
            {
                string sql = "select [MakId],[MajorKindId],[MajorKindName],[MajorId],[MajorName] from [dbo].[CM] ";
                return connection.Query<CM>(sql).ToList();
            }
        }
    }
}
